function [pX,pY,ODraw] = ProcessImagev2(hdfFilePath,autoROI,userROI,doFitData)

% [pX,pY,ODraw] = ProcessImage(hdfFilePath,autoROI,userROI,doFitData)
% Finds Gaussian-shaped signals on fluorescence or absorption images.
% Returns:
% pX - The parameters for a Gaussian fit to the data in the x (vertical)
% direction.
% pX(1) = Gaussian amplitude
% pX(2) = Peak position (in units of image pixels)
% pX(3) = Standard deviation (in units of image pixels)
% pX(4) = Offset
% pY - Same description as pX
% ODraw - The image (OD if it's an absorption image, raw image for a fluor.)

% Inputs from hdf5 file
rawimages = cast(h5read(hdfFilePath,'/Image/image'),'double');
img_var = h5read(hdfFilePath,'/Image/configuration');
img_time = h5read(hdfFilePath,'/Image/exposuretime');
type_img = h5read(hdfFilePath,'/Image/typeimaging');
CCDbin = 1; % Amount of binning at camera sensor; '1' is 1x1 binning and '2' is 2x2 binning

%Software binning
bindim=[1,1];  % Software binning size
rawimages=BinImg2d(rawimages,bindim);

% Libraries used in this function
expp = ParamImgSys(img_var);
cons = Constants();

dim = size(rawimages);  % size of hdf5 image group
rows = dim(1);          % rows in one image file (# of horiz. pixels)
cols = dim(2);          % columns in one image file (# of vert. pixels)

ovflw = 'no';   % setting 'ovflw' flag to 'no'
if  type_img == 3 % Absorption  %%size(dim,2)
    elec = rawimages(:,:,3);
    num = rawimages(:,:,1) - elec;
    den = rawimages(:,:,2) - elec;
    maxcntN = max(max(num));
    maxcntD = max(max(den));
    if maxcntN > 2^16 || maxcntD > 2^16
        ovflw = 'yes';
        warning('Bin overflow warning');
    end
    
    %scaling the baselevel of the atom image
    numforscale=10;  %using a 10x10 pixel area to determine scaling fraction
    numleftup = sum(sum(num(1:numforscale,1:numforscale)));
    numleftdn = sum(sum(num(rows-numforscale:rows,1:numforscale)));
    numrightup = sum(sum(num(1:numforscale,cols-numforscale:cols)));
    numrightdn = sum(sum(num(rows-numforscale:rows,cols-numforscale:cols)));
    baselevelnum = numleftup + numleftdn + numrightup + numrightdn;
    
    denleftup = sum(sum(den(1:numforscale,1:numforscale)));
    denleftdn = sum(sum(den(rows-numforscale:rows,1:numforscale)));
    denrightup = sum(sum(den(1:numforscale,cols-numforscale:cols)));
    denrightdn = sum(sum(den(rows-numforscale:rows,cols-numforscale:cols)));
    baselevelden = denleftup + denleftdn + denrightup + denrightdn;
    blscale = (baselevelden/baselevelnum); %resulting baselevel scaling
    modnum=num*blscale;
    ODraw = -log((num./den)');  %SIMON ROTATES THE MATRIX HERE: (num./den)'
    ODrawmod = -log(modnum./den);
    
elseif type_img == 1 % Fluorescence
    ODraw = rawimages';
    ODraw = ODraw - min(min(ODraw));
else
    error('Can''t determine imaging method');
end

% Filter image to find ROI
N = 60;
sig = 30;
ODfilt = conv2(ODraw,gaussian2d(N,sig),'same'); % Low-pass filter image

yC = 1:expp.short_px_nb/CCDbin;  %SIMON DEFINES y-axis AS HORIZ. DIMENSION OF SENSOR
xC = 1:expp.long_px_nb/CCDbin;

if autoROI % Find ROI and extract first approx. signal from filtered image
    % Determine pre-ROI for fitting (for initial guesses)
    mx = max(max(ODfilt));
    [I,J] = find(ODfilt == mx);
    wndw = 50;
    ROI(1) = I(1);
    ROI(2) = J(1);
    
    % Projections of ODfilt using pre-ROI as size (for initial guesses)
    yIntFilt = sum(ODfilt(:,ROI(2)-wndw:ROI(2)+wndw),2);
    xIntFilt = sum(ODfilt(ROI(1)-wndw:ROI(1)+wndw,:),1);
    
    % Least-squares fit of ODfilt (initial guesses)
    ampGuessY = max(yIntFilt) - min(yIntFilt);
    ampGuessX = max(xIntFilt) - min(xIntFilt);
    options = optimset('Display','off','TolFun',1e-16,'TolX',1e-16);
    pYtmp = fminsearch(@(p)FitGauss(p,yC,yIntFilt),[ampGuessY ROI(1) 100 min(yIntFilt)],options);
    pXtmp = fminsearch(@(p)FitGauss(p,xC,xIntFilt'),[ampGuessX ROI(2) 100 min(xIntFilt)],options);
     
    % Determining autoROI based on size of ODfilt (for real fit)
    sigmas = 3;
    xWndw = round(sigmas*pXtmp(3));
    yWndw = round(sigmas*pYtmp(3));
    xROI = ROI(2)-xWndw:ROI(2)+xWndw;
    yROI = ROI(1)-yWndw:ROI(1)+yWndw;
    
    % Remove parts of the ROI if it goes outside of the photo.
    if min(xROI) < 1
        xROI = xROI(xROI > 0);
    end
    if min(yROI) < 1
        yROI = yROI(yROI > 0);
    end
    
    if max(yROI) > expp.short_px_nb
        yROI = yROI(yROI < expp.short_px_nb + 1);
    end
    if max(xROI) > expp.long_px_nb
        xROI = xROI(xROI < expp.long_px_nb + 1);
    end
    
    % Projections of ODraw using autoROI as size (for real fit)
    yInt = sum(ODraw(yROI,xROI),2);
    xInt = sum(ODraw(yROI,xROI),1);
   
    
else  % Use user-input ROI parameters to guess initial fitting parameters
    % User-defined ROI
    yROI = userROI(3):userROI(4);
    xROI = userROI(1):userROI(2);
    
    % Projections of ODraw using user-defined ROI as size (for real fit)
    yInt = sum(ODraw(yROI,xROI),2);
    xInt = sum(ODraw(yROI,xROI),1);
    
    % Amp. and sizes (initial guesses)
    pYtmp(1) = max(yInt) - min(yInt);
    pXtmp(1) = max(xInt) - min(xInt);
    pYtmp(2:3) = [mean(userROI(3:4)) 150];
    pXtmp(2:3) = [mean(userROI(1:2)) 150];
end

if doFitData % Real fit of ODraw using initial guesses
    options = optimset('Display','off','TolFun',1e-16,'TolX',1e-16);
    pY = fminsearch(@(p)FitGauss(p,yROI,yInt),[pYtmp(1:3) min(yInt)],options);
    pX = fminsearch(@(p)FitGauss(p,xROI,xInt'),[pXtmp(1:3) min(xInt)],options);
else
    pY = zeros(1,4);
    pX = pY;
end

% Give the width as the std deviation
widthYm = (pY(3))*expp.px_size*CCDbin/expp.mag;    %Y cloud size (m)
widthXm = (pX(3))*expp.px_size*CCDbin/expp.mag;    %X cloud size (m)
amp = (pX(1) + pY(1))/2;                    % "2D" Gaussian fit amplitude (mean of single Gaussian fits)

if type_img == 1 % Fluorescence
    % Estimate number of atoms using camera
    total_num_electrons = amp*sqrt(2*pi)*mean([pX(3) pY(3)]);
    total_photons = total_num_electrons/(expp.qe*expp.coll_eff);
    num_atoms = 2*total_photons/(img_time*1e-6*cons.Gamma) % num_atoms = (num_photons/time) / (scattering rate)
elseif type_img == 3 % Absorption
    totalODraw = sum(sum(ODraw))*(expp.px_size*CCDbin)^2;
    num_atomsOD = totalODraw/AbsCross(0,0);
    %num_atomsFit = amp*2*pi*widthYm*widthXm/AbsCross(0,0);
end

if type_img == 1 % Fluorescence
    figure(1)
    subplot(3,3,[1 2 4 5])
    oneMat = ones(size(ODraw));
    zeroMat = nan(size(ODraw));
    zeroMat(yROI,xROI) = 1;
    ODwindow = ODraw.*zeroMat;
    imagesc(ODwindow);
    % imagesc((ODfilt));
    line([xROI(1) xROI(end)],[yROI(1) yROI(1)])
    line([xROI(1) xROI(1)],[yROI(1) yROI(end)])
    line([xROI(end) xROI(end)],[yROI(1) yROI(end)])
    line([xROI(1) xROI(end)],[yROI(end) yROI(end)])
    axis equal
    xlims = xlim;
    ylims = ylim;
    
    subplot(3,3,[3 6])
    plot(yInt,yROI,gauss(pY,yC),yC);
    set(gca,'YDir','reverse');
    ylim(ylims);
    
    subplot(3,3,[7 8])
    plot(xROI,xInt,xC,gauss(pX,xC));
    xlim(xlims);
    
    h = subplot(3,3,9,'Visible', 'off');
    cla(h);
    text(0, .5, sprintf('%s\n%s\n%s\n%s', ...
        ['v-width (um): ' num2str(widthXm*cons.um)], ['h-width (um): ' num2str(widthYm*cons.um)], ...
        ['peak(v,h) = (' num2str(round(pX(2))) ',' num2str(round(pY(2))) ')'], ...
        ['Na = ' num2str(num_atoms,'%e')]), ...
        'VerticalAlignment', 'middle', ...
        'FontName', 'Courier New', ...
        'FontWeight', 'bold', ...
        'FontSize', 13);
elseif type_img == 3 % Absorption
    figure(1)
    subplot(4,3,[1 2 4 5 ])
    oneMat = ones(size(ODraw));
    zeroMat = nan(size(ODraw));
    zeroMat(yROI,xROI) = 1;
    ODwindow = ODraw.*zeroMat;
    imagesc(ODwindow);
    % imagesc((ODfilt));
    line([xROI(1) xROI(end)],[yROI(1) yROI(1)])
    line([xROI(1) xROI(1)],[yROI(1) yROI(end)])
    line([xROI(end) xROI(end)],[yROI(1) yROI(end)])
    line([xROI(1) xROI(end)],[yROI(end) yROI(end)])
    axis equal
    xlims = xlim;
    ylims = ylim;
    
    subplot(4,3,[3 6])
    plot(yInt,yROI,gauss(pY,yC),yC);
    set(gca,'YDir','reverse');
    ylim(ylims);
    
    subplot(4,3,[7 8])
    plot(xROI,xInt,xC,gauss(pX,xC));
    xlim(xlims);
    
    subplot(4,3,10)
    imagesc(num')
    axis equal
    
    subplot(4,3,11)
    imagesc(den')
    axis equal
    
    subplot(4,3,12)
    imagesc(elec')
    axis equal
    
    h = subplot(4,3,9,'Visible', 'off');
    cla(h);
    text(0, .6, sprintf('%s\n%s\n%s\n%s\n%s\n%s \n%s \n%s', ...
        ['v-width (um): ' num2str(widthXm*cons.um)], ['h-width (um): ' num2str(widthYm*cons.um)], ...
        ['peak(v,h) = (' num2str(round(pX(2))) ',' num2str(round(pY(2))) ')'], ...
        ['N(image) = ' num2str(num_atomsOD,'%e')], ...
        ['peak pixel cnt = ' num2str(max(max(den)))], ...
        ['Peak OD(image) = ' num2str(max(max(ODraw)))], ...
        ['Peak OD(fit) = ' num2str(1)], ...
        ['Mean OD(image) = ' num2str(mean(mean(ODraw)))]), ...
        'VerticalAlignment', 'middle', ...
        'FontName', 'Courier New', ...
        'FontWeight', 'bold', ...
        'FontSize', 13);
end



% figure(3)
% imagesc(rawimages(:,:,3))
% line([xROI(1) xROI(end)],[yROI(1) yROI(1)])
% line([xROI(1) xROI(1)],[yROI(1) yROI(end)])
% line([xROI(end) xROI(end)],[yROI(1) yROI(end)])
% line([xROI(1) xROI(end)],[yROI(end) yROI(end)])
% axis equal

% cnt = numel(rawimages(:,:,2));
%
% sigAt = reshape(rawimages(:,:,1),1,cnt); % signal with atoms
% sigBK = reshape(rawimages(:,:,2),1,cnt); % background signal (no atoms)
%
% [Nat,pAt] = hist(sigAt,100);
% [Nbk,pBK] = hist(sigBK,100);
%
% figure(2)
% plot(pAt,Nat,pBK,Nbk)
%
% figure(3)
% imagesc(rawimages(:,:,1))